package com.yto.hrreport.event.buttons {
	import com.adobe.serialization.json.JSON;
	import com.yto.hrreport.command.sysTemplatePanel;
	import com.yto.hrreport.event.buttons.superbutton.ButtonEvent;
	import com.yto.hrreport.pub.IProcessor;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.containers.HDividedBox;
	import mx.controls.Alert;
	import mx.controls.DataGrid;
	import mx.controls.Tree;
	import mx.managers.PopUpManager;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	public class TempSaveButtonEvent extends ButtonEvent {
		
		private static var saveEvent : TempSaveButtonEvent = new TempSaveButtonEvent();
		private static var parentName : String;
		private var headCanvas : Canvas;
		private var action : String;
		private var sysTree : Tree;
		
		public function TempSaveButtonEvent() {
			super();
		}
		
		public static function getInstance(parentName : String) : TempSaveButtonEvent {
			TempSaveButtonEvent.parentName = parentName;
			
			return saveEvent;
			
		}
		
		public function save(action : String) : void {
			this.action = action;
			saveHeadAndBodyTable();
		}
		
		private function saveHeadAndBodyTable() : void {
			
			var parentCanvas : Canvas = Canvas(hrreport.ui.mainNavigator.getChildByName(parentName + "_canvas"));
			headCanvas = Canvas(parentCanvas.getChildByName(parentName + "_headCanvas"));
			var bodyCanvas : Canvas = Canvas(parentCanvas.getChildByName(parentName + "_bodyCanvas"));
			var hdivBox : HDividedBox = HDividedBox(bodyCanvas.getChildByName(parentName + "_hdivBox"));
			sysTree = Tree(hdivBox.getChildByName(parentName + "_sysTree"));
			var template : sysTemplatePanel = sysTemplatePanel.getInstance();
			
			var item : Object = sysTree.selectedItem;
			
			if(ArrayCollection(template.headPosGrid.dataProvider).length > 0 || ArrayCollection(template.bodyPosGrid.dataProvider).length > 0) {
				
				PopUpManager.addPopUp(hrreport.waitWindow , hrreport.ui , true);
				PopUpManager.centerPopUp(hrreport.waitWindow);
				
				var tableServer : RemoteObject = new RemoteObject();
				tableServer.destination = IProcessor.TableProcessor;
				tableServer.insertHeadTable(item.nodeid , item.nodename);	
				tableServer.addEventListener(ResultEvent.RESULT , saveHead);
			}
		}
		
		private function saveHead(event : ResultEvent) : void {
			
			var lastResult : String = event.result as String;
			
			if(lastResult != null) {
				
				var tableServer : RemoteObject = new RemoteObject();
				
				switch(action) {
					
					case "tempAdd" :
						
						var template : sysTemplatePanel = sysTemplatePanel.getInstance();
						var array : Array = new Array();
						
						var headData : ArrayCollection = template.headPosGrid.dataProvider as ArrayCollection;
						var bodyData : ArrayCollection = template.bodyPosGrid.dataProvider as ArrayCollection;
						
						setData(array , "head" , lastResult , headData);
						setData(array , "body" , lastResult , bodyData);
						
						var jsonData : String = JSON.encode(array);
						
						tableServer.destination = IProcessor.TableProcessor;
						tableServer.insertBodyTable(jsonData);	
						tableServer.addEventListener(ResultEvent.RESULT , saveBody);
						
						break;
					
					case "tempEdit" : 
						
						tableServer.destination = IProcessor.TableProcessor;
						tableServer.deleteBodyTableByPK(lastResult);	
						tableServer.addEventListener(ResultEvent.RESULT , afterDeleteBody);
						
						break;
					
					default :
						
						break;
				
				}
			} else {
				PopUpManager.removePopUp(hrreport.waitWindow);
				Alert.show("保存表头数据异常，保存失败" , "提示" , Alert.OK , hrreport.ui);
			}
			
		}
		
		private function afterDeleteBody(event : ResultEvent) : void {
			
			var lastResult : String = event.result as String;
			
			if(lastResult != null && lastResult != "false") {
				
				var template : sysTemplatePanel = sysTemplatePanel.getInstance();
				var array : Array = new Array();
				
				var headData : ArrayCollection = template.headPosGrid.dataProvider as ArrayCollection;
				var bodyData : ArrayCollection = template.bodyPosGrid.dataProvider as ArrayCollection;
				
				setData(array , "head" , lastResult , headData);
				setData(array , "body" , lastResult , bodyData);
				
				var jsonData : String = JSON.encode(array);
				
				var tableServer : RemoteObject = new RemoteObject();
				tableServer.destination = IProcessor.TableProcessor;
				tableServer.insertBodyTable(jsonData);	
				tableServer.addEventListener(ResultEvent.RESULT , saveBody);
				
			} else {
				
				PopUpManager.removePopUp(hrreport.waitWindow);
				Alert.show("保存数据异常，保存失败" , "提示" , Alert.OK , hrreport.ui);
				
			}
			
		}
		
		private function setData(array : Array, pos : String , pk_table : String , dataProvider : ArrayCollection) : void {
			
			for each(var data : Object in dataProvider) {
				var item : Object = new Object();
				item.pk_table = pk_table;
				item.fieldcode = data.fieldcode;
				item.fieldname = data.fieldname;
				item.pos = pos;
				item.pk_table_b = data.pk_table_b;
				
				array.push(item);
				
			}
		}
		
		private function saveBody(event : ResultEvent) : void {
			
			var lastResult : String = event.result as String;
			
			if(lastResult != null) {
			
				var template : sysTemplatePanel = sysTemplatePanel.getInstance();
				
				template.reset();
				
				var headArray : Array = new Array();
				var bodyArray : Array = new Array();
				
				var resultArray : Array = JSON.decode(lastResult) as Array;
				filterData(headArray , bodyArray , resultArray);
				template.headPosGrid.dataProvider = new ArrayCollection(headArray);
				template.bodyPosGrid.dataProvider = new ArrayCollection(bodyArray);
				
				sysTree.enabled = true;
				
				updateButtons(null , "sysTree_ItemChange_template" , headCanvas , true);
			
			} else {
				Alert.show("保存数据异常，保存失败" , "提示" , Alert.OK , hrreport.ui);
			}
			
			PopUpManager.removePopUp(hrreport.waitWindow);
			
		}
		
		private function filterData(headArray : Array , bodyArray : Array , resultArray : Array) : void {
			
			for each(var item : Object in resultArray) {
				
				if(item.pos == "head")
					headArray.push(item);
				else if(item.pos == "body")
					bodyArray.push(item);
				
			}
			
		}
		
	}
}